﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace FeedReader.Shared.Data
{
    public class BlogIDDataSource
    {
        private BlogIDDataServiceContext _ServiceContext = null;

        public BlogIDDataSource(CloudStorageAccount storageAccount)
        {
            _ServiceContext = new BlogIDDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            // Create the tables
            // In this case, just a single table.  
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(BlogIDDataServiceContext.BlogIDTableName);
        }

        public IEnumerable<BlogIDDataModel> Select()
        {
            var results = from c in _ServiceContext.BlogIDTable
                          select c;

            var query = results.AsTableServiceQuery<BlogIDDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }

        public void Insert(BlogIDDataModel newItem)
        {
            _ServiceContext.AddObject(BlogIDDataServiceContext.BlogIDTableName, newItem);
            _ServiceContext.SaveChanges();
        }

        //public IEnumerable<BlogIDDataModel> SelectByDate(DateTime date)
        //{
        //    var results = from c in _ServiceContext.BlogIDTable
        //                  where c.ModiefiedDateTime<date||c.EarliestDateTime<date
        //                  select c;

        //    var query = results.AsTableServiceQuery<BlogIDDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}

        //public IEnumerable<BlogIDDataModel> FindByURL(string url)
        //{
        //    var results = from c in this.Select()
        //                  where c.BlogURL ==url
        //                  select c;

        //    return results;
        //}

        //public IEnumerable<BlogIDDataModel> FindName(string name)
        //{
        //    var results = from c in _ServiceContext.BlogIDTable
        //                  where c.BlogID == name
        //                  select c;

        //    var query = results.AsTableServiceQuery<BlogIDDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}

        //public void Delete(BlogIDDataModel itemToDelete)
        //{
        //    _ServiceContext.AttachTo(BlogIDDataServiceContext.BlogIDTableName, itemToDelete, "*");
        //    _ServiceContext.DeleteObject(itemToDelete);
        //    _ServiceContext.SaveChanges();
        //}


    }
}
